<?php
namespace app\home\controller;

use think\Controller;
use think\Session;

class Base extends Controller {

    function __construct(){
        Session::start();
        header("Cache-control: private");
        parent::__construct();
    }

    public function _initialize(){
        $this->config();
        $this->user();
        $this->dispose();
        $this->complete();
        $this->cancel_meal_order();
        $this->cancel_shop_order();
    }

    //配置文件
    public function config(){
        $config = array();
        $m_config = M('config')->cache(true,MT_CACHE_TIME)->select();
        foreach($m_config as $k => $v){
            if($v['name'] == 'hot_keywords'){
                $config['hot_keywords'] = explode('|', $v['value']);
            }
            $config[$v['inc_type'].'_'.$v['name']] = $v['value'];
        }
        $this->assign('config', $config);
    }

    //判断是否登陆
    public function user(){
        $where['openid'] = session('openid');
        if(!empty(trim($where['openid']))){
            $user = M('user')->where($where)->find();
            //unset($user['verification']);
            unset($user['password']);
            return $user;
        }
        return false;
    }


    public function getCode(){
        $redirect_uri = urlencode('http://xg.wm61.mingtengnet.com/home/index/get_info.html');
        $wx_config = C('wx_config');
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$wx_config['appid'].'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
        header("Location:".$url);
    }


    //微信名字处理
    public function filterEmoji($str)
    {
        $str = preg_replace_callback( '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $str);
        return $str;
    }

    //预约订单处理
    public function dispose(){
        $list = M('order_subscribe')->where(array('orderStatus' => 0))->select();
        foreach ($list as $key=>$val){
            $time = $val['orderAddTime'] + 300;
            if (time() > $time){
                M('order_subscribe')->where(array('orderSubscribeId' => $val['orderSubscribeId']))->save(array('orderStatus' => -1));
            }
        }
    }

    //完成订单
    public function complete(){
        $list = M('order_subscribe')->where(array('orderStatus' => 1))->select();
        foreach ($list as $key=>$val){
            $subscribe_time = $val['orderStartTime'] + (65 * 60);
            if (time() > $subscribe_time){
                M('order_subscribe')->where(array('orderSubscribeId' => $val['orderSubscribeId']))->save(array('orderStatus' => 2));
            }
        }

    }

    //商场订单处理
    public function cancel_shop_order(){
        $list = M('shop_order')->where(array('order_status' => 0))->select();
        foreach ($list as $key=>$val){
            $time = $val['add_time'] + 60*15;
            if (time() > $time){
                M('shop_order')->where(array('order_id' => $val['order_id']))->save(array('order_status' => -1));
            }
        }
    }

    //订餐订单处理
    public function cancel_meal_order(){
        $list = M('meal_order')->where(array('order_status' => 0))->select();
        foreach ($list as $key=>$val){
            $time = $val['add_time'] + 300;
            if (time() > $time){
                M('meal_order')->where(array('order_id' => $val['order_id']))->save(array('order_status' => -1));
            }
        }
    }




}
